#include "multi_thread_tracker.h"

void Multi_Thread_Tracker::detect()
{
	//cv::Mat detect_frame;
	std::vector<cv::Point2f> centers;
	std::vector<cv::Rect> regions;

	while (true)
	{
		if (shared_frame.empty())
			continue;

		/*frame_mutex.lock();
		if (detect_frame.empty())
			detect_frame = shared_frame.clone();
		else
			shared_frame.copyTo(detect_frame);
		frame_mutex.unlock();*/

		centers.clear();
		regions.clear();

		detector->detect(shared_frame, centers, regions);
		std::vector<CRegion> cregions(regions.begin(), regions.end());
		std::vector<Point_t> centers_t(centers.begin(), centers.end());

		detect_result ldetect_result = std::make_pair(std::move(centers_t), std::move(cregions));

		bool succeed = detect_queue.try_enqueue(ldetect_result);
		if (!succeed)
		{
			detect_result ldiscard_result;
			detect_queue.try_dequeue(ldiscard_result);
			detect_queue.enqueue(ldetect_result);
		}
	}
}

void Multi_Thread_Tracker::update(cv::Mat image)
{
	if (shared_frame.empty())
		shared_frame = image.clone();
	else
		image.copyTo(shared_frame);

	detect_result ldetect_result;

	detect_queue.try_dequeue(ldetect_result);
	
	tracker->Update(ldetect_result.first, ldetect_result.second, image);
}
